#!/usr/bin/env node

/**
 * Configuration Migration Tool
 * Migrates configuration files from legacy locations to standard cross-platform locations
 */

const path = require('path');
const ConfigPathResolver = require('../lib/config/config-path-resolver');

function main() {
    console.log('🔄 Vision Analyzer 配置迁移工具');
    console.log('====================================\n');

    const projectRoot = path.resolve(__dirname, '..');
    const pathResolver = new ConfigPathResolver();

    // 显示平台信息
    const platformInfo = pathResolver.getPlatformInfo();
    console.log('📋 平台信息:');
    console.log(`   系统: ${platformInfo.platform}`);
    console.log(`   主目录: ${platformInfo.homeDir}`);
    console.log(`   标准配置目录: ${platformInfo.configDir}`);
    console.log(`   标准配置文件: ${platformInfo.configFilePath}\n`);

    // 执行迁移
    console.log('🔄 开始配置迁移...');
    const migrationResult = pathResolver.migrateFromLegacy(projectRoot);

    if (migrationResult.migrated) {
        console.log('✅ 配置迁移成功!');
        console.log(`   源文件: ${migrationResult.legacyPath}`);
        console.log(`   目标文件: ${migrationResult.standardPath}\n`);

        console.log('💡 建议下一步操作:');
        console.log('   1. 验证新配置文件内容是否正确');
        console.log('   2. 测试Vision Analyzer是否正常工作');
        console.log('   3. 如果一切正常，可以删除旧配置文件\n');
    } else {
        console.log('ℹ️  无需迁移或迁移跳过');
        console.log(`   原因: ${migrationResult.reason}`);

        if (migrationResult.legacyPath) {
            console.log(`   源路径: ${migrationResult.legacyPath}`);
        }
        if (migrationResult.standardPath) {
            console.log(`   目标路径: ${migrationResult.standardPath}`);
        }
        if (migrationResult.error) {
            console.log(`   错误: ${migrationResult.error}`);
        }
        console.log();
    }

    // 检查当前配置状态
    console.log('📊 当前配置状态:');
    const currentConfig = pathResolver.getBestConfigPath(projectRoot);

    if (currentConfig) {
        console.log(`   当前配置文件: ${currentConfig.path}`);
        console.log(`   配置来源: ${currentConfig.source}`);

        // 检查文件大小和修改时间
        try {
            const fs = require('fs');
            const stats = fs.statSync(currentConfig.path);
            console.log(`   文件大小: ${stats.size} 字节`);
            console.log(`   修改时间: ${stats.mtime.toLocaleString()}`);
        } catch (error) {
            console.log(`   ⚠️  无法读取文件信息: ${error.message}`);
        }
    } else {
        console.log('   ⚠️  未找到任何配置文件');
        console.log('   💡 请运行 "npm run setup" 创建初始配置');
    }

    console.log('\n🎯 标准配置路径说明:');
    console.log('   • Windows: %LOCALAPPDATA%\\vision-analyzer\\Config\\settings.json');
    console.log('   • macOS: ~/Library/Preferences/vision-analyzer/settings.json');
    console.log('   • Linux: ~/.config/vision-analyzer/settings.json');
}

if (require.main === module) {
    main();
}

module.exports = { main };